﻿var c = document.getElementById('canvas').getContext('2d'),	//The Canvas Context to draw on
	particles = [],											//All particles (ammo) that should be drawn
	asteroids = [],											//All asteroids that should be drawn
	explosions = [],										//All explosions that should be drawn
	infoTexts = [],											//All infotexts that should be drawn
	ships = [],												//All living ships (self + opponents)
	deadVessels = [],										//All dead ships (self + opponents)
	ticks = 0,												//Current game time
	gameRunning = false,									//If the game is currently running
	loop,													//The drawing loop
	socket,													//The webserver connection
	explosionImg,											//The image (sprite) used for an explosion
	implosionImg,											//The image (sprite) used for another explosion
	shiposionImg,											//The image (sprite) used for another explosion
	asteroidImg,											//The image used for an asteroid
	primaryColors = ['127,255,0','255,102,153','148,0,211','139,69,19'],	//The color palette (first colors)
	secondaryColors = ['192,255,62','255,51,51','138,43,226','160,82,45'],	//The color palette	(second colors for gradients)
	multiPlayer = false;									//GameMode (SinglePlayer w/o Network, Multiplayer)
	
var 	MAX_AMMO = 50,										//Maximum amount of ammo that can be carried
		INIT_COOLDOWN = 10,									//Cooldown between two shots
		MAX_LIFE = 100,										//Maximum amount of life (of the ship)
		MAX_PARTICLE_TIME = 200,							//Maximum time that a particle (ammo) is alive
		ASTEROID_LIFE = 10,									//Initial life of an asteroid
		MAX_SPEED = 6,										//Maximum speed of the sheep
		DAMAGE_FACTOR = 12,									//Maximum damage per shot
		ACCELERATE = 0.1;									//Acceleration per round

var collision = function(sx, sy, sw, sh, dx, dy, dw, dh) {	//Method to detect collisions
	var sleft = sx - sw / 2;
	var sright = sx + sw / 2;
	var sup = sy - sh / 2;
	var sbottom = sy + sh / 2;
	var dleft = dx - dw / 2;
	var dright = dx + dw / 2;
	var dup = dy - dh / 2;
	var dbottom = dy + dh / 2;
	
	if(sleft > dright)
		return false;
	else if(sright < dleft)
		return false;
	else if(sup > dbottom)
		return false;
	else if(sbottom < dup)
		return false;
	
	return true;
};

var d2g = function(angle) {		//Helper to calculate degree -> rad
	return Math.PI / 180 * angle;
};

var getShip = function(id) {	//Gets a ship based on the ID - also searches in dead vessels
	for(var i in ships)
		if(ships[i].id === id)
			return ships[i];
	for(var i in deadVessels)
		if(deadVessels[i].id === id)
			return deadVessels[i];
};

var handlefocus = function(e) {	//Gets focus to the canvas
	if (e.type === 'mouseover')
		c.canvas.focus();
	else 
		return true;
	return false;
};

var manageKeyboard = function(key, status) {	//Manages the keyboard
	switch(key) {
		case 37:
			keyboard.left = status;
			break;
		case 38:
			keyboard.up = status;
			break;
		case 39:
			keyboard.right = status;
			break;
		case 40:
			keyboard.down = status;
			break;
		case 32:
			keyboard.shoot = status;
			break;
		default:
			return;	//Not a meaningful key - just return!
	}
};